using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using JNPF.Basics.Models;
using JNPF.Basics.Logic;
using JNPF.Utils;
using System.Threading.Tasks;
using System.Linq;
using JNPF.Utils.Controller;
using JNPF.Basics.Logic.Common.Login;
using JNPF.Utils.Extensions;

namespace JNPF.Basics.Controller
{
    [Route("api/Jiushi/[Controller]")]
    public class CustomerloandetailController : BaseApiController
    {
        private CustomerloandetailBll jiushiCustomerloandetailService = new CustomerloandetailBll();




        private CustomerloanBll customerloanBll = new CustomerloanBll();
        private BillRuleBll billRuleService = new BillRuleBll();
        private DataInterfaceBll dataInterfaceBll = new DataInterfaceBll();
        private UserBll userBll = new UserBll();
        private OrganizeBll organizeBll = new OrganizeBll();


        #region GET

        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(int id)
        {
            var data = await jiushiCustomerloandetailService.GetInfo(id);
            var output = data.MapTo<CustomerloandetailUpOutput>();
            CustomerloanEntity customerloanEntity = await customerloanBll.GetInfoByUUId(data.CustomerLoanUUID);
            output.customerloanEntity = customerloanEntity.MapTo<CustomerloanCrInput>();
            if (!data.Rate.HasValue)
            {
                data.Rate = output.customerloanEntity.rate;
            }

           
            return Success(output);
        }


        /// <summary>
        /// 根据需求ID返回贷款记录
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpGet("GetCustomerNeedRecordId/{id}")]
        public async Task<ResponseResult> GetCustomerNeedRecordId(int id)
        {
            var data = await jiushiCustomerloandetailService.GetDetialByneedDetailId(id);

            return Success(data);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResponseResult> GetList(CustomerloandetailListQueryInput input)
        {
            var data = await jiushiCustomerloandetailService.GetPageList(input);

            //贷款数据
            List<string> lis = data.list.Select(x => x.CustomerLoanUUID).ToList();
            List<CustomerloanEntity> customerloans = await customerloanBll.GetInfoByUUId(lis);

            var outputList = data.list.MapToList<CustomerloandetailListOutput>();
            outputList.ForEach(o =>
            {
                // o.customerloanEntity = customerloans.Find(x => x.UUID == o.customerLoanUUID).MapTo<CustomerloanCrInput>();
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }





        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetPageListByCus")]
        public async Task<ResponseResult> GetPageListByCus(CustomerloandetailListQueryInput input)
        {
            var data = await jiushiCustomerloandetailService.GetPageListByCus(input);
            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }



        /// <summary>
        /// 获取列表  企业用户  ===  手机号码对应起来的 
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetBusPageList")]
        public async Task<ResponseResult> GetBusPageList(CustomerloandetailListQueryInput input)
        {
            var data = await jiushiCustomerloandetailService.GetBusPageList(input);
            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }
        /// <summary>
        /// 客户经理列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetCuseBusList")]
        public async Task<ResponseResult> GetCuseBusList(CustomerloandetailListQueryInput input)
        {
            var data = await jiushiCustomerloandetailService.GetCuseBusList(input);
            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 信贷管家   列表（分页）
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetPagCuseList")]
        public async Task<ResponseResult> GetPagCuseList(CustomerloandetailListQueryInput input)
        {
            var data = await jiushiCustomerloandetailService.GetPagCuseList(input);
            var output = new PageResult()
            {
                list = data.list,
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("GetListBybankUserId")]
        public async Task<ResponseResult> GetListBybankUserId(CustomerloandetailListQueryInput input)
        {
            var data = await jiushiCustomerloandetailService.GetPageListBySql(input);


            //贷款数据
            List<string> lis = data.list.Select(x => x.CustomerLoanUUID).ToList();
            List<CustomerloanEntity> customerloans = await customerloanBll.GetInfoByUUId(lis);

            var outputList = data.list.MapToList<CustomerloandetailListOutput>();
            outputList.ForEach(o =>
            {
                // o.customerloanEntity = customerloans.Find(x => x.UUID == o.customerLoanUUID).MapTo<CustomerloanCrInput>();
            });
            var output = new PageResult()
            {
                list = outputList,
                pagination = data.pagination
            };
            return Success(output);
        }


        #endregion

        #region POST

        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="input">新建输入</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] CustomerloandetailCrInput input)
        {
            var entity = input.MapTo<CustomerloandetailEntity>();
            await jiushiCustomerloandetailService.Create(entity);
            return Success("新建成功");
        }



        /// <summary>
        /// 放款
        /// </summary>
        /// <param name="id">参数</param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("Reviewer/{id}")]
        public async Task<ResponseResult> Lending(int id, [FromBody] CustomerloanCrInputNews input)
        {
            var entity = await jiushiCustomerloandetailService.GetInfo(id);
            if (entity == null)
            {
                return Error("当前信息有误");
            }
            if (entity.Type != 0)
            {
                return Error("当前状态不允许放款");
            }

            await jiushiCustomerloandetailService.Lending(entity, input);

            return Success("放款成功");
        }


        /// <summary>
        /// 续贷
        /// </summary>
        /// <param name="id">参数</param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("RenewalLoan/{id}")]
        public async Task<ResponseResult> RenewalLoan(int id, [FromBody] CustomerloanCrInputNews input)
        {
            var entity = await jiushiCustomerloandetailService.GetInfo(id);
            if (entity == null)
            {
                return Error("当前信息有误");
            }
            if (entity.Type != 1)
            {
                return Error("当前状态不允许续贷");
            }
            await jiushiCustomerloandetailService.RenewalLoan(entity, input);
            return Success("续贷成功");
        }

        /// <summary>
        /// 结清
        /// </summary>
        /// <param name="id">参数</param>
        /// <param name="input"></param>
        /// <returns></returns> 
        [HttpPut("EndLending/{id}")]
        public async Task<ResponseResult> EndLending(int id, [FromBody] EndLendingInput input)
        {
            var entity = await jiushiCustomerloandetailService.GetInfo(id);
            if (entity == null)
            {
                return Error("当前信息有误");
            }
            if (entity.Type != 1)
            {
                return Error("当前状态不允许结清");
            }

            entity.EndDate = input.endLendingdate;


            entity.EndLendingAmount = input.endLendingAmount;
            entity.EndLendingDesc = input.endLendingDesc;
            entity.EndAttachment = input.endAttachment;
            entity.Type = 3;

            await jiushiCustomerloandetailService.Update(id, entity);
            return Success("结清成功");
        }


        /// <summary>
        /// 终止
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpPost("End/{id}")]
        public async Task<ResponseResult> End(int id)
        {
            var entity = await jiushiCustomerloandetailService.GetInfo(id);
            if (entity == null)
            {
                return Error("当前信息有误");
            }
            if (entity.Type != 0)
            {
                return Error("当前状态不允许结清");
            }
            entity.EndDate = DateTime.Now;
            entity.Type = 4;
            await jiushiCustomerloandetailService.Update(id, entity);
            return Success("终止成功");
        }


        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键id</param>
        /// <param name="input">编辑输入</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(int id, [FromBody] CustomerloandetailCrInput input)
        {
            var entity = input.MapTo<CustomerloandetailEntity>();
            var isOk = await jiushiCustomerloandetailService.Update(id, entity);
            return isOk > 0 ? Success("修改成功") : Error("修改失败");
        }

        /// <summary>
        /// 删除接口
        /// </summary>
        /// <param name="id">主键id</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(int id)
        {
            var entity = await jiushiCustomerloandetailService.GetInfo(id);
            var isOk = await jiushiCustomerloandetailService.Delete(entity);
            return isOk > 0 ? Success("删除成功") : Error("删除失败");
        }

        #endregion

    }
}